<template>
  <el-dialog
    :title="formData.id ? '编辑单词' : '新增单词'"
    :visible="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    center
  >
    <el-form
      ref="addEditForm"
      v-loading="isLoading > 0"
      :model="formData"
      :rules="rules"
      label-width="100px"
      class="add-edit-form"
    >
      <el-form-item label="单词" prop="word">
        <el-input v-model="formData.word" />
      </el-form-item>
      <el-form-item label="单词发音" prop="audio_word">
        <el-button type="primary" @click="resetWordSpeechWR">重置（微软）</el-button>
      </el-form-item>
      <el-form-item label="翻译" prop="translate">
        <el-input v-model="formData.translate" />
      </el-form-item>
      <el-form-item label="注解" prop="comment">
        <el-input v-model="formData.comment" />
      </el-form-item>
      <el-form-item label="图片" prop="images">
        <upload-img v-model="formData.images" :size="0.5" />
      </el-form-item>

      <el-form-item label="是否上线">
        <el-switch
          :value="formData.status === 1"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="formData.status = formData.status === 1 ? -1 : 1"
        />
      </el-form-item>

      <el-form-item class="submit-box">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button v-if="!formData.id" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
const emptyFormData = {
  word: null,
  audio_word: null,
  translate: null,
  comment: null,
  images: null,
  status: -1
}
let formData = null
import _ from 'lodash'
import UploadImg from '@/components/UploadImg'
import { handleFormData, removeNoChangeValue } from '@/utils/tools'
import { addOneWord, updateOneWord, resetWordSpeechWR } from '@/api/word'
export default {
  name: 'WordAddEdit',
  components: {
    UploadImg
  },
  props: {},
  data() {
    return {
      dialogVisible: false,
      isLoading: 0,
      formData: {},
      rules: {
        word: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {},
  methods: {
    handleClose() {
      this.dialogVisible = false
    },
    open(row) {
      this.dialogVisible = true
      if (row) {
        formData = _.cloneDeep(row)
      } else {
        formData = _.cloneDeep(emptyFormData)
      }
      this.resetForm()
    },
    resetForm() {
      this.formData = _.cloneDeep(formData)
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    submitForm() {
      if (this.isLoading > 0) return
      this.$refs.addEditForm.validate(async vaild => {
        if (!vaild) return
        try {
          this.isLoading++
          handleFormData(this.formData)
          if (this.formData.id) {
            const _formData = removeNoChangeValue(this.formData, formData)
            if (_formData === false) {
              this.$message.error('您没有修改任何值！')
              return
            }
            await updateOneWord(_formData)
          } else {
            await addOneWord(this.formData)
          }
          this.$emit('refresh')
          this.handleClose()
        } finally {
          this.isLoading--
        }
      })
    },
    resetWordSpeechWR() {
      this.$confirm('确定要进行此次操作么？', '温馨提示').then(async() => {
        try {
          this.isLoading++
      await resetWordSpeechWR(this.formData.id)
          await this.getWordList()
        } finally {
          this.isLoading--
        }
      }).catch(() => {
        console.log('取消')
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.submit-box {
  display: flex;
  justify-content: flex-end;
}
</style>
